package com.kongjian.manage.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.kongjian.manage.common.R;
import com.kongjian.manage.model.dto.NewWaterLevelRecordsDTO;
import com.kongjian.manage.model.query.StorageCapacityQuery;
import com.kongjian.manage.model.vo.CalcWarehousingVO;
import com.kongjian.manage.model.vo.StorageCapacityVO;
import com.kongjian.manage.service.IStorageCapacityService;
import com.kongjian.manage.service.IWaterLevelRecordsService;
import com.kongjian.manage.utils.ExcelUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author tangjunfeng
 * @Classname StorageCapacityController.java
 * @Description 库容controller
 * @Date 2023-10-20
 * @Version V1.0
 */
@Api(tags = "霍河水库每日水情")
@RestController
@Validated
@RequestMapping("/api/v1/storage-capacity")
@CrossOrigin
public class StorageCapacityController {


    @Autowired
    private IStorageCapacityService storageCapacityService;

    @Autowired
    private IWaterLevelRecordsService waterLevelRecordsService;

    @ApiOperationSupport(order = 1)
    @GetMapping("query-water-level-and-storage")
    @ApiOperation(value = "查询水位库容(查询前一天)", hidden = true)
    public List<StorageCapacityVO> queryWaterLevel() {
        return storageCapacityService.query();
    }


    @ApiOperationSupport(order = 2)
    @PostMapping("query-water-level-and-storage-by-time")
    @ApiOperation("查询水位库容(通过时间段)")
    public List<StorageCapacityVO> queryWaterLevelByTime(@RequestBody StorageCapacityQuery storageCapacityQuery) {
        return storageCapacityService.query(storageCapacityQuery);
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "批量新增")
    @PostMapping("add-list")
    public R<String> addWaterLevelRecordsList(@RequestBody @Validated List<NewWaterLevelRecordsDTO> list) throws Exception {
        for (NewWaterLevelRecordsDTO newWaterLevelRecordsDTO : list) {
            waterLevelRecordsService.addWaterLevelRecords(newWaterLevelRecordsDTO);
        }
        return R.success("新增成功");
    }


    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "计算入库值")
    @PostMapping("calc-warehousing")
    public BigDecimal calcWarehousing(@RequestBody CalcWarehousingVO vo) {
        // =(D6-D5)/0.36+F6+G6+H6+I6
        BigDecimal tmp = new BigDecimal("0.36");
        BigDecimal divide = vo.getStorageCapacityVal().subtract(vo.getPreStorageCapacityVal()).divide(tmp, 1, BigDecimal.ROUND_HALF_UP);
        BigDecimal add = divide.add(vo.getPower()).add(vo.getWater()).add(vo.getEcologyFlow()).add(vo.getDownFlow());

        return add;
    }

    @ApiOperationSupport(order = 5)
    @PostMapping("export-ignore")
    @ApiOperation("导出（废弃）")
    @ApiIgnore
    public void exportIgnore(@RequestBody StorageCapacityQuery storageCapacityQuery, HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<StorageCapacityVO> result = storageCapacityService.query(storageCapacityQuery);
        ExcelUtil.exportExcel(result, null, "霍河水库每日水情", StorageCapacityVO.class, "export.xlsx", response);
    }

    @ApiOperationSupport(order = 5)
    @PostMapping("export")
    @ApiOperation("导出")
    public void export(@RequestBody StorageCapacityQuery storageCapacityQuery, HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<StorageCapacityVO> result = storageCapacityService.query(storageCapacityQuery);
        storageCapacityService.export(result,response);
    }



}
